{% macro heading(params) %}
    {% if params.level is not defined %}
        {% set params = params|merge({'level': 1}) %}
    {% endif %}

    <h{{ params.level }}{% if 1 == params.level %} class="mt-3"{% endif %}>
        {% if params.link is defined %}
            {{ _self.link({'url': params.link.url, 'title': params.title|raw})}}
        {% else %}
            {{ params.title|raw }}
        {% endif %}

        {% if params.badge is defined %}
            {% set badge = params.badge %}

            {% if badge.context is not defined %}
                {% set badge = badge|merge({'context': 'light'}) %}
            {% endif %}

            {% if 2 == params.level %}
                <small>{{ _self.badge(badge) }}</small>
            {% else %}
                {{ _self.badge(badge) }}
            {% endif %}
        {% endif %}
    </h{{ params.level }}>
{% endmacro %}

{% macro link(params) %}
    {% if params.class is not defined %}
        {% set params = params|merge({'class': 'text-' ~ theme_color_2}) %}
    {% endif %}

    <a class="{{ params.class }}" href="{{ params.url }}">{{ params.title }}</a>
{% endmacro %}

{% macro sort(params) %}
    {% if true == params.cat_sort %}
        {% if params.class is not defined %}
            {% set params = params|merge({'class': 'text-' ~ theme_color_2}) %}
        {% endif %}

        {% if '?' in params.url %}
            {% set url = params.url ~ '&sort=' ~ params.sort %}
        {% else %}
            {% set url = params.url ~ '?sort=' ~ params.sort %}
        {% endif %}

        {% if app.request.query.get('sort') and app.request.query.get('sort') == params.sort ~ ':asc' %}
            {% set url = url ~ ':desc' %}
        {% elseif false == app.request.query.get('sort') and params.default is defined and params.default == ':asc' %}
            {% set url = url ~ ':desc' %}
        {% else %}
            {% set url = url ~ ':asc' %}
        {% endif %}
        <a class="{{ params.class }}" href="{{ url }}">
            {{ params.title }}
            {% set badge = false %}
            {% if app.request.query.get('sort') and (app.request.query.get('sort') == params.sort ~ ':asc' or app.request.query.get('sort') == params.sort ~ ':desc') %}
                {% set badge = app.request.query.get('sort') %}
            {% elseif false == app.request.query.get('sort') and params.default is defined %}
                {% set badge = params.default %}
            {% endif %}
            {% if badge %}
                {% if ':asc' in badge %}
                    {{ _self.badge({'title': 'sort_asc'|trans, 'context': 'secondary'}) }}
                {% else %}
                    {{ _self.badge({'title': 'sort_desc'|trans, 'context': 'secondary'}) }}
                {% endif %}
            {% endif %}
        </a>
    {% else %}
        {{ params.title }}
    {% endif %}
{% endmacro %}

{% macro dashboardKpi(params) %}
    <div class="text-center mb-4 col-12 col-md-4 col-lg-3">
        <h4>{{ params.title }}</h4>
        <h3>{{ _self.badge(params.badge) }}</h3>
    </div>
{% endmacro %}

{% macro button(params) %}
    {% if params.context is not defined %}
        {% set params = params|merge({'context': 'primary'}) %}
    {% endif %}

    <a href="{{ params.url }}" class="btn btn-{{ params.context }} btn-sm"{% if params.target is defined %} target="{{ params.target }}"{% endif %}>
        {{ params.title }}
    </a>
{% endmacro %}

{% macro buttonModal(params) %}
    {% if params.id is not defined %}
        {% set params = params|merge({'id': ''}) %}
    {% endif %}

    {% if params.title is not defined %}
        {% set params = params|merge({'title': ''}) %}
    {% endif %}

    {% if params.body is not defined %}
        {% set params = params|merge({'body': ''}) %}
    {% endif %}

    {% if params.href is not defined %}
        {% set params = params|merge({'href': ''}) %}
    {% endif %}

    {% if params.button is not defined %}
        {% set params = params|merge({'button': 'primary'}) %}
    {% endif %}

    {% if params.size is not defined %}
        {% set params = params|merge({'size': 'sm'}) %}
    {% endif %}

    <button type="button" class="btn btn-{{ params.button }} btn-{{ params.size }}" data-bs-toggle="modal" data-bs-target="#modal{{ params.id }}">
        {{ params.title }}
    </button>
    <div class="modal fade" id="modal{{ params.id }}" tabindex="-1" role="dialog" aria-labelledby="modalLabel{{ params.id }}" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content {{ theme_modal_content }}">
                <div class="modal-header">
                    <h5 class="modal-title" id="modalLabel{{ params.id }}">{{ params.title }}{% if params.badge is defined %}{{ _self.badge(params.badge) }}{% endif %}</h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="{{ 'cancel'|trans }}"></button>
                </div>
                <div class="modal-body text-left">
                    <p>
                        {{ params.body|raw }}
                    </p>
                    {% if params.help is defined %}
                        <p{% if params.help_type is defined %} class="text-{{ params.help_type }}"{% endif %}>
                            {{ params.help|raw }}
                        </p>
                    {% endif %}
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ 'cancel'|trans }}</button>
                    <a href="{{ params.href }}" class="btn btn-{{ params.button }}">{{ 'confirm'|trans }}</a>
                </div>
            </div>
        </div>
    </div>
{% endmacro %}

{% macro form(params) %}
    {% if params.type is not defined %}
        {% set params = params|merge({'type': 'default'}) %}
    {% endif %}

    {% if params.action is not defined %}
        {% set params = params|merge({'action': app.request.uri}) %}
    {% endif %}

    {{ form_start(params.form, {'action': params.action}) }}
        <div class="row">
            {% if 'search' == params.type %}<div class="col-12 col-md-4 col-lg-3">{% else %}<div class="col-12 col-md-5 col-lg-4">{% endif %}
                {% for child in params.form.children %}
                    {{ form_row(child) }}
                    {% if child.vars.attr is defined and child.vars.attr['data-break-after'] is defined and 'yes' == child.vars.attr['data-break-after'] %}
                        </div>
                        {% if 'search' == params.type %}<div class="col-12 col-md-4 col-lg-3">{% else %}<div class="col-12 col-md-5 col-lg-4">{% endif %}
                    {% endif %}
                {% endfor %}
            </div>
            <div class="col-12">
                <div class="form-group mb-3">
                    <button type="submit" class="btn btn-primary">{% if 'search' == params.type %}{{ 'search'|trans }}{% else %}{{ 'submit'|trans }}{% endif %}</button>
                </div>
            </div>
        </div>
    {{ form_end(params.form) }}
{% endmacro %}

{% macro paginator(params) %}
    {% if 1 < params.pages %}
        {% set parameters = params.route_parameters|merge(app.request.query.all) %}

        <ul class="pagination">
            {% if params.previous %}
                <li class="page-item">
                    <a class="page-link bg-{{ theme_color_1 }} text-{{ theme_color_2 }}" rel="prev" href="{{ path(params.route, parameters|merge({'page': params.previous})) }}">&laquo;&nbsp;{{ 'previous'|trans }}</a>
                </li>
            {% else %}
                <li class="page-item disabled">
                    <span class="page-link bg-{{ theme_color_1 }} text-secondary">&laquo;&nbsp;{{ 'previous'|trans }}</span>
                </li>
            {% endif %}

            {% if params.first %}
                <li class="page-item">
                    <a class="page-link bg-{{ theme_color_1 }} text-{{ theme_color_2 }}" rel="next" href="{{ path(params.route, parameters|merge({'page': params.first})) }}">{{ params.first }}</a>
                </li>
                {% if params.first != params.start_page - 1 %}
                    <li class="page-item disabled">
                        <span class="page-link bg-{{ theme_color_1 }} text-secondary">&hellip;</span>
                    </li>
                {% endif %}
            {% endif %}

            {% for page in params.pages_slice %}
                {% if page != params.page %}
                    <li class="page-item">
                        <a class="page-link bg-{{ theme_color_1 }} text-{{ theme_color_2 }}" href="{{ path(params.route, parameters|merge({'page': page})) }}">{{ page }}</a>
                    </li>
                {% else %}
                    <li class="page-item active">
                        <span class="page-link bg-{{ theme_color_2 }} text-{{ theme_color_1 }} border border-{{ theme_color_2 }}">{{ page }}</span>
                    </li>
                {% endif %}
            {% endfor %}

            {% if params.last %}
                {% if params.last != params.end_page + 1 %}
                    <li class="page-item disabled">
                        <span class="page-link bg-{{ theme_color_1 }} text-secondary">&hellip;</span>
                    </li>
                {% endif %}
                <li class="page-item">
                    <a class="page-link bg-{{ theme_color_1 }} text-{{ theme_color_2 }}" rel="next" href="{{ path(params.route, parameters|merge({'page': params.last})) }}">{{ params.last }}</a>
                </li>
            {% endif %}

            {% if params.next %}
                <li class="page-item">
                    <a class="page-link bg-{{ theme_color_1 }} text-{{ theme_color_2 }}" rel="next" href="{{ path(params.route, parameters|merge({'page': params.next})) }}">{{ 'next'|trans }}&nbsp;&raquo;</a>
                </li>
            {% else %}
                <li class="page-item disabled">
                    <span class="page-link bg-{{ theme_color_1 }} text-secondary">{{ 'next'|trans }}&nbsp;&raquo;</span>
                </li>
            {% endif %}
        </ul>
    {% endif %}
{% endmacro %}

{% macro getPercent(total, partial, round) %}
{% apply spaceless %}
    {% if(total > 0) %}
        {% set percent = (partial * 100) / total %}
    {% else %}
        {% set percent = 0 %}
    {% endif %}

    {{ percent|round(round) }}
{% endapply %}
{% endmacro %}

{% macro badge(params) %}
    {% set contextColor = _self.getContextColor(params) %}

    {% if params.link is defined %}
        <a class="badge bg-{{ contextColor }}{% if contextColor in ['info', 'light', 'warning', 'strongwarning'] %} link-dark{% else %} link-light{% endif %}" href="{{ params.link }}">{{ params.title }}</a>
    {% else %}
        <span class="badge bg-{{ contextColor }}{% if contextColor in ['info', 'light', 'warning', 'strongwarning'] %} text-dark{% else %} text-light{% endif %}">{{ params.title }}</span>
    {% endif %}
{% endmacro %}

{% macro getContextColor(params) %}
{% apply spaceless %}
    {% if params.context is not defined %}
        {% set params = params|merge({'context': 'info'}) %}
    {% endif %}

    {% if params.context in ['true', '1'] %}
        {% set params = params|merge({'context': 'secondary'}) %}

    {% elseif params.context in ['false', '0'] %}
        {% set params = params|merge({'context': 'light'}) %}

    {% elseif params.context in ['green', 'open', 'started', 'running', 'success', 'audit_pass', 'yes'] %}
        {% set params = params|merge({'context': 'success'}) %}

    {% elseif params.context in ['red', 'stopped', 'unassigned', 'failed', 'incompatible', 'close', 'audit_fail', 'watermark_flood_stage', 'no', 'critical'] %}
        {% set params = params|merge({'context': 'danger'}) %}

    {% elseif params.context in ['watermark_high', 'partial', 'orange'] %}
        {% set params = params|merge({'context': 'strongwarning'}) %}

    {% elseif params.context in ['yellow', 'stopping', 'relocating', 'in_progress', 'watermark_low', 'throttle', 'throttled', 'warning'] %}
        {% set params = params|merge({'context': 'warning'}) %}

    {% elseif params.context in ['initializing', 'audit_notice'] %}
        {% set params = params|merge({'context': 'light'}) %}

    {% elseif params.context in ['master_node'] %}
        {% set params = params|merge({'context': 'masternode'}) %}
    {% endif %}

    {{ params.context }}
{% endapply %}
{% endmacro %}
